Haskell, GTK4 and list views

As a digression to my experiments with GTK4 and Haskell, inspired by a question at the Haskell Community, I looked at list views.

A list view brings together a selection model and a list item factory. A selection model adds support for selection to a list model. A list item factory creates widgets for the list items of a list model. The properties of a list item include an item from the list model, the position of the item in the list model, and the child widget used to display the item.

A list of strings

A simple list model is a value of type StringList. Its items are values of type StringObject. A simple selection model is a value of type SingleSelection.

An example of a list item factory is a value of type SignalListItemFactory. It emits signals, such as setup and bind, to manage list items.

Following a suggestion by Barry Fishman, an alternative to nested whenJust is the use of MaybeT, as follows:

The result was as follows:

A list of values of another type

A more complex list model is a value of type GI.Gio.Objects.ListStore, provided by the gi-gio package. Two key functions are:

All the items in the list model are of the same type, which must be an instance of type class IsObject and, consequently, an instance of type classes GObject, TypedObject and HasParentTypes. Types that are instances of GObject are specified with newtype and have a data constructor of type ManagedPtr o -> o.

ParentTypes is an open indexed type family:

Class TypedObject promises glibType :: IO GType, which is implemented using registerGType. That, in turn, requires the type to be an instance of DerivedGObject.

The modified program is:

The result was as follows: